﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Security;
using CS795.TravelAgency.Domain.Entities;

namespace CS795.TravelAgency.Infrastructure
{
    public class CustomRoleProvider : RoleProvider
    {
        private Domain.TravelAgencyContext context = new Domain.TravelAgencyContext();

        private string m_ApplicationName;
        public override string ApplicationName
        {
            get
            {
                return m_ApplicationName;
            }
            set
            {
                m_ApplicationName = value;
            }
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            Domain.Entities.User user = context.Users.Include("Roles").Where(u => u.Name == username).SingleOrDefault();
            if (user != null)
            {
                if (user.Roles.Select(r => r.Name).Contains(roleName))
                {
                    return true;
                }
            }
            return false;
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            foreach (string username in usernames)
            {
                Domain.Entities.User user = context.Users.Include("Roles").Where(u => u.Name == username).SingleOrDefault();
                if (user != null)
                {
                    user.Roles.RemoveAll(r => context.Roles.Where(c => roleNames.Contains(c.Name)).Select(a => a.Name).Contains(r.Name));
                }
            }
            context.SaveChanges();
        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            foreach (string username in usernames)
            {
                Domain.Entities.User user = context.Users.Include("Roles").Where(u => u.Name == username).SingleOrDefault();
                if (user != null)
                {
                    IQueryable<Domain.Entities.Role> roles = context.Roles.Where(r => roleNames.Contains(r.Name));
                    user.Roles.AddRange(roles);
                }
            }
            context.SaveChanges();
        }

        public override string[] GetUsersInRole(string roleName)
        {
            Domain.Entities.Role role = context.Roles.Where(r => r.Name == roleName).SingleOrDefault();
            if (role != null)
            {
                return role.Users.Select(u => u.Name).ToArray();
            }
            return null;
        }

        public override string[] GetAllRoles()
        {
            return context.Roles.Select(r => r.Name).ToArray();
        }

        public override string[] GetRolesForUser(string username)
        {
            Domain.Entities.User user = context.Users.Include("Roles").Where(u => u.Name == username).SingleOrDefault();
            if (user != null)
            {
                return user.Roles.Select(r => r.Name).ToArray();
            }
            return null;
        }

        public override bool RoleExists(string roleName)
        {
            return context.Roles.Select(r => r.Name).Contains(roleName);
        }

        #region Not Implemented Functions
        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}